package com.link.base.base.report.controller;

import com.link.base.base.report.model.ActiveConversionRate;
import com.link.base.base.report.service.ActiveConversionRateService;
import com.link.core.basic.annotations.JsonParam;
import com.link.core.basic.controller.BasicController;
import com.link.core.basic.model.BasicModel;
import com.link.core.basic.query.QueryParams;
import com.link.core.basic.service.BasicService;
import com.link.core.basic.service.ServiceException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 活动转化率-
 *
 * @author 黄正杨
 * @date 2019/03/08 13:48
 */
@Controller
@RequestMapping("/link/activeConversionRate")
public class ActiveConversionRateController extends BasicController<ActiveConversionRate> {


    @Resource
    private ActiveConversionRateService activeConversionRateService;

    @Override
    public BasicService<ActiveConversionRate> getBasicService() throws Exception {
        //避免调用增删改接口
        throw new ServiceException("REPORT-001");
    }

    /**
     * 刷新活动转化率报表的数据
     *
     * @return Map
     * @author 黄正杨
     * @date 2019/03/12
     */
    @ResponseBody
    @RequestMapping("/synchronizationData")
    public Map<String, Object> synchronizationData() {
        Map<String, Object> result = new HashMap<>(4);
        try {
            activeConversionRateService.synchronizationData();
            result.put("success", true);
            result.put("code", "200");
        } catch (ServiceException e) {
            result.put("success", false);
            result.put("result", e.getMessage());
            result.put("detailMessage", e.getMessage());
            result.put("code", e.getCode());
        } catch (Exception e) {
            result.put("success", false);
            result.put("result", e.getMessage());
            result.put("code", "400");
        }
        return result;
    }

    @Override
    public Map<String, Object> queryByExamplePage(@JsonParam QueryParams qps, @JsonParam ActiveConversionRate entity,
                                                  HttpSession session, HttpServletRequest request,
                                                  HttpServletResponse response) {
        HashMap<String, Object> result = new HashMap<>(8);
        try {
            qps.preDealSecurity(request);
            ActiveConversionRate temp = BasicModel.transformClass(entity, qps);
            List<ActiveConversionRate> list = activeConversionRateService.queryByExamplePage(temp);
            result.put("success", true);
            result.put("rows", list);
            this.pushCurrentDataCache(list, session.getId(), "base", false);
            result.put("total", getCount(temp, list.size()));
            result.put("code", "200");
        } catch (ServiceException e) {
            result.put("success", false);
            result.put("rows", new ArrayList());
            result.put("result", e.getMessage());
            result.put("detailMessage", e.getMessage());
            result.put("code", e.getCode());
        } catch (Exception e) {
            result.put("success", false);
            result.put("rows", new ArrayList());
            result.put("result", e.getMessage());
            result.put("code", "400");
        }
        return result;
    }
}
